#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , showLocalData(true)
{
    setWindowTitle("CPU使用率监控 - 自定义折线图");
    resize(1000, 700);

    cpuMonitor = new CPUMonitor(this);
    sshMonitor = new SSHMonitor(this);

    // 设置ui界面
    setupUI();
    // 连接信号
    connect(cpuMonitor, &CPUMonitor::dataUpdated, this, &MainWindow::onCPUDataUpdated);
    connect(sshMonitor, &SSHMonitor::dataUpdated, this, &MainWindow::onSSHDataUpdated);
    connect(sshMonitor, &SSHMonitor::connectionStatusChanged, this, &MainWindow::onSSHConnectionStatusChanged);
    connect(sshMonitor, &SSHMonitor::errorOccurred, this, &MainWindow::onSSHErrorOccurred);

}

// 设置ui界面
void MainWindow::setupUI() {
    // 创建中央部件
    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);

    // 标题
    QLabel *titleLabel = new QLabel("CPU使用率实时监控", this);
    QFont titleFont = titleLabel->font();
    titleFont.setPointSize(16);
    titleFont.setBold(true);
    titleLabel->setFont(titleFont);
    titleLabel->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(titleLabel);

    // 图表区域
    chartWidget = new ChartWidget(this);
    chartWidget->setMinimumSize(800, 400);
    mainLayout->addWidget(chartWidget);

    // 详细信息区域
    QHBoxLayout *infoLayout = new QHBoxLayout();

    // 创建标签
    totalLabel = new QLabel("总CPU使用率: 0.0%", this);
    userLabel = new QLabel("用户态CPU: 0.0%", this);
    systemLabel = new QLabel("系统态CPU: 0.0%", this);
    idleLabel = new QLabel("空闲CPU: 0.0%", this);

    // 设置标签样式
    QFont labelFont = totalLabel->font();
    labelFont.setPointSize(12);
    totalLabel->setFont(labelFont);
    userLabel->setFont(labelFont);
    systemLabel->setFont(labelFont);
    idleLabel->setFont(labelFont);

    // 设置标签颜色
    totalLabel->setStyleSheet("color: #2E86AB; font-weight: bold;");
    userLabel->setStyleSheet("color: #A23B72; font-weight: bold;");
    systemLabel->setStyleSheet("color: #F18F01; font-weight: bold;");
    idleLabel->setStyleSheet("color: #C73E1D; font-weight: bold;");

    // 添加到布局
    infoLayout->addWidget(totalLabel);
    infoLayout->addWidget(userLabel);
    infoLayout->addWidget(systemLabel);
    infoLayout->addWidget(idleLabel);
    infoLayout->addStretch();

    mainLayout->addLayout(infoLayout);

    // 添加远程监控UI
    setupRemoteUI();
    mainLayout->addWidget(remoteGroupBox);

    // 添加数据源切换UI
    setupDataSourceUI();
    mainLayout->addWidget(dataSourceGroupBox);
}

// 更新数据
void MainWindow::onCPUDataUpdated(CPUData data)
{
    // 根据数据源选择决定是否更新图表
    if (showLocalData) {
        chartWidget->updateData(data, true); // true表示本地数据
        updateLabels(data);
    }
}

// 更新标签
void MainWindow::updateLabels(CPUData data)
{
    totalLabel->setText(QString("总CPU使用率: %1%").arg(data.total, 0, 'f', 1));
    userLabel->setText(QString("用户态CPU: %1%").arg(data.user, 0, 'f', 1));
    systemLabel->setText(QString("系统态CPU: %1%").arg(data.system, 0, 'f', 1));
    idleLabel->setText(QString("空闲CPU: %1%").arg(data.idle, 0, 'f', 1));
}

// 设置远程监控UI
void MainWindow::setupRemoteUI() {
    remoteGroupBox = new QGroupBox("SSH远程监控", this);

    QGridLayout *layout = new QGridLayout(remoteGroupBox);

    //SSH配置
    layout->addWidget(new QLabel("SSH主机："), 0, 0);
    hostEdit = new QLineEdit(this);
    hostEdit->setPlaceholderText("例如：192.168.1.100");
    layout->addWidget(hostEdit, 0, 1);

    layout->addWidget(new QLabel("用户名："), 1, 0);
    userEdit = new QLineEdit(this);
    userEdit->setPlaceholderText("例如：root");
    layout->addWidget(userEdit, 1, 1);

    // 连接按钮
    connectButton = new QPushButton("SSH连接", this);
    disconnectButton = new QPushButton("SSH断开", this);
    disconnectButton->setEnabled(false);

    // 水平布局
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(connectButton);
    buttonLayout->addWidget(disconnectButton);
    layout->addLayout(buttonLayout, 2, 0, 1, 2);

    // 状态标签
    connectionStatusLabel = new QLabel("状态：未连接", this);
    connectionStatusLabel->setStyleSheet("color:red;front-weight:blod");
    layout->addWidget(connectionStatusLabel, 3, 0, 1, 2);

    //远程CPU数据显示
    remoteTotalLabel = new QLabel("远程总CPU：0.0%", this);
    remoteUserLabel = new QLabel("远程用户态：0.0%", this);
    remoteSystemLabel = new QLabel("远程系统态：0.0%", this);
    remoteIdleLabel = new QLabel("远程空闲：0.0%", this);

    QHBoxLayout *remoteInfoLayout = new QHBoxLayout;
    remoteInfoLayout->addWidget(remoteTotalLabel);
    remoteInfoLayout->addWidget(remoteUserLabel);
    remoteInfoLayout->addWidget(remoteSystemLabel);
    remoteInfoLayout->addWidget(remoteIdleLabel);
    layout->addLayout(remoteInfoLayout, 4, 0, 1, 2);

    // 连接信号槽
    connect(connectButton, &QPushButton::clicked, this, &MainWindow::connectToSSH);
    connect(disconnectButton, &QPushButton::clicked, this, &MainWindow::disconnectFromSSH);
}

// 设置数据源切换UI
void MainWindow::setupDataSourceUI() {
    dataSourceGroupBox = new QGroupBox("数据源切换", this);

    QHBoxLayout *layout = new QHBoxLayout(dataSourceGroupBox);

    // 创建切换按钮
    toggleDataBtn = new QPushButton("当前：本地数据", this);

    // 设置按钮样式
    toggleDataBtn->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; border: none; padding: 8px 16px; border-radius: 4px; } QPushButton:hover { background-color: #45a049; }");

    // 添加到布局
    layout->addWidget(toggleDataBtn);
    layout->addStretch();

    // 连接信号槽
    connect(toggleDataBtn, &QPushButton::clicked, this, &MainWindow::onToggleDataSelected);
}

// SSH连接
void MainWindow::connectToSSH() {
    QString host = hostEdit->text().trimmed();
    QString user = userEdit->text().trimmed();

    if(host.isEmpty() || user.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入SSH主机地址和用户名");
        return;
    }
    // 连接的时候不可二次点击
    connectButton->setEnabled(false);
    connectionStatusLabel->setText("状态：连接中...");
    connectionStatusLabel->setStyleSheet("color:orange;font-weight:bold");

    if(sshMonitor->connectToHost(host, user)) {
        // 连接的时候，点击断开连接有用
        connectButton->setEnabled(false);
        disconnectButton->setEnabled(true);
        connectionStatusLabel->setText("状态：已连接");
        connectionStatusLabel->setStyleSheet("color:greeen;font-weight:bold");
    }else {
        connectButton->setEnabled(true);
        disconnectButton->setEnabled(false);
        connectionStatusLabel->setText("状态：连接失败");
        connectionStatusLabel->setStyleSheet("color:red;font-weight:bold");
    }
}

//SSH断开
void MainWindow::disconnectFromSSH() {
    sshMonitor->disconnect();
    connectButton->setEnabled(true);
    disconnectButton->setEnabled(false);
    connectionStatusLabel->setText("状态：未连接");
    connectionStatusLabel->setStyleSheet("color:red;font-weight:bold");

    // 清空远程数据显示
    updateSSHLabels(CPUData());
}

// SSH数据更新
void MainWindow::onSSHDataUpdated(const CPUData &data)
{
    updateSSHLabels(data);
    // 根据数据源选择决定是否更新图表
    if (!showLocalData) { // 当不显示本地数据时，显示远程数据
        chartWidget->updateData(data, false); // false表示远程数据
    }
}
// SSH连接状态变化
void MainWindow::onSSHConnectionStatusChanged(bool connected)
{
    if (connected) {
        connectButton->setEnabled(false);
        disconnectButton->setEnabled(true);
        connectionStatusLabel->setText("状态: 已连接");
        connectionStatusLabel->setStyleSheet("color: green; font-weight: bold;");
    } else {
        connectButton->setEnabled(true);
        disconnectButton->setEnabled(false);
        connectionStatusLabel->setText("状态: 未连接");
        connectionStatusLabel->setStyleSheet("color: red; font-weight: bold;");
    }
}

// SSH错误处理
void MainWindow::onSSHErrorOccurred(const QString &error)
{
    QMessageBox::warning(this, "SSH错误", error);
    disconnectFromSSH();
}

// 更新SSH标签
void MainWindow::updateSSHLabels(const CPUData &data)
{
    remoteTotalLabel->setText(QString("远程总CPU: %1%").arg(data.total, 0, 'f', 1));
    remoteUserLabel->setText(QString("远程用户态: %1%").arg(data.user, 0, 'f', 1));
    remoteSystemLabel->setText(QString("远程系统态: %1%").arg(data.system, 0, 'f', 1));
    remoteIdleLabel->setText(QString("远程空闲: %1%").arg(data.idle, 0, 'f', 1));
}

// 数据源切换槽函数
void MainWindow::onToggleDataSelected()
{
    if (showLocalData) {
        showLocalData = false;
        toggleDataBtn->setText("当前：远程数据");
        toggleDataBtn->setStyleSheet("QPushButton { background-color: #2196F3; color: white; border: none; padding: 8px 16px; border-radius: 4px; } QPushButton:hover { background-color: #1976D2; }");
        chartWidget->clearData(); // 清空图表，重新开始显示远程数据
        qDebug() << "切换到远程数据显示";
    } else {
        showLocalData = true;
        toggleDataBtn->setText("当前：本地数据");
        toggleDataBtn->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; border: none; padding: 8px 16px; border-radius: 4px; } QPushButton:hover { background-color: #45a049; }");
        chartWidget->clearData(); // 清空图表，重新开始显示本地数据
        qDebug() << "切换到本地数据显示";
    }
}
